package com.litchi.tutor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.litchi.tutor.common.entity.Deliver;
import com.litchi.tutor.common.entity.Teacher;
import com.litchi.tutor.common.entity.User;
import com.litchi.tutor.common.util.JwtUtil;
import com.litchi.tutor.mapper.DeliverMapper;
import com.litchi.tutor.mapper.TeacherMapper;
import com.litchi.tutor.mapper.UserMapper;
import com.litchi.tutor.service.DeliverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xyouzi
 * @since 2021-11-22
 */
@Service
public class DeliverServiceImpl extends ServiceImpl<DeliverMapper, Deliver> implements DeliverService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    TeacherMapper teacherMapper;

    @Override
    public void autoDeliverByTeacher(Integer recruitId , HttpServletRequest request) {
        Deliver deliver = new Deliver();
        String token = request.getHeader("Authorization");
        Integer id = JwtUtil.getId(token);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", id));
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("userid", user.getUserid()));
        Integer teacherId = teacher.getId();
        Deliver one = getOne(new QueryWrapper<Deliver>().eq("recruit_id", recruitId).eq("teacher_id", teacherId).ne("status", 6));
        if (one == null) {
            // 数据库没有当前recruit_id和teacher_id的数据或者状态是【家长拒绝】（也就是status=6)，插入一条新数据，状态为【老师发起投递】
            deliver.setRecruitId(recruitId);
            deliver.setTeacherId(teacherId);
            deliver.setStatus(1);
            save(deliver);
            // TODO 消息通知
        }
        else {
            // 如果有数据
            if (one.getStatus() == 1 || one.getStatus() == 9) {
                // 如果状态是【老师发起投递 / 双方已接受】，则什么都不做 return
                return;
                // TODO 消息通知
            }
            if (one.getStatus() == 5) {
                // 如果状态是【家长发起招募】，则更新状态为【双方已接受】
                deliver.setStatus(9);
                update(deliver, new QueryWrapper<Deliver>().eq("id", one.getId()));
                // TODO 消息通知
            }
            if (one.getStatus() == 2) {
                // 如果状态是【老师拒绝】，则更新状态为【老师发起投递】
                deliver.setStatus(1);
                update(deliver, new QueryWrapper<Deliver>().eq("id", one.getId()));
                // TODO 消息通知
            }
        }
    }

    @Override
    public void autoDeliverByParent(Integer recruitId, Integer teacherId, HttpServletRequest request) {
        Deliver deliver = new Deliver();
        Deliver one = getOne(new QueryWrapper<Deliver>().eq("recruit_id", recruitId).eq("teacher_id", teacherId).ne("status", 2));
        if (one == null) {
            // 数据库没有当前recruit_id和teacher_id的数据或者状态是【家长拒绝】（也就是status=6)，插入一条新数据，状态为【老师发起投递】
            deliver.setRecruitId(recruitId);
            deliver.setTeacherId(teacherId);
            deliver.setStatus(1);
            save(deliver);

            // TODO 消息通知
        }
        else {
            // 如果有数据
            if (one.getStatus() == 5 || one.getStatus() == 9) {
                // 如果状态是【家长发起招募 / 双方已接受】，则什么都不做 return
                return;
                // TODO 消息通知
            }
            if (one.getStatus() == 1) {
                // 如果状态是【老师发起投递】，则更新状态为【双方已接受】
                deliver.setStatus(9);
                update(deliver, new QueryWrapper<Deliver>().eq("id", one.getId()));
                // TODO 消息通知
            }
            if (one.getStatus() == 6) {
                // 如果状态是【家长拒绝】，则更新状态为【家长发起招募】
                deliver.setStatus(1);
                update(deliver, new QueryWrapper<Deliver>().eq("id", one.getId()));
                // TODO 消息通知
            }
        }
    }

    /**
     *  家长查询自己的被投递状态列表
     *  不传：全部
     *  1：未受理
     *  2：已拒绝
     *  3：已接受
     *
     *  老师查询自己的投递状态列表
     *  不传：全部
     *  1：已投递
     *  2：被拒绝
     *  3：已完成
     * @param statue
     * @return
     */
    @Override
    public List<Deliver> queryDeliverByTeacherOrParent(Integer statue) {
        return getDeliver(statue);
    }

    /**
     * 老师查询被招募状态列表
     *  不传：全部
     *  1：未受理
     *  2：已拒绝
     *  3：已接受
     *
     * 家长查询自己的招募状态列表
     *  不传：全部
     *  1：已招募
     *  2：被拒绝
     *  3：已完成
     * @param statue
     * @return
     */
    @Override
    public List<Deliver> queryRecruitedByTeacherOrParent(Integer statue) {
        return getRecruit(statue);
    }

    // 获取 家长查询自己的被投递状态列表 和 老师查询自己的投递状态列表
    private List<Deliver> getDeliver(Integer statue) {
        List<Deliver> list = null;
        if (statue == null) {
            list = list();
            return list;
        }
        if (statue == 1) {
            list = list(new QueryWrapper<Deliver>().eq("status", 1));
        }
        if (statue == 2) {
            list = list(new QueryWrapper<Deliver>().eq("status", 6));
        }
        if (statue == 3) {
            list = list(new QueryWrapper<Deliver>().eq("status", 9));
        }
        return list;
    }

    // 获取 老师查询被招募状态列表 和 家长查询自己的招募状态列表
    private List<Deliver> getRecruit(Integer statue) {
        List<Deliver> list = null;
        if (statue == null) {
            list = list();
            return list;
        }
        if (statue == 1) {
            list = list(new QueryWrapper<Deliver>().eq("status", 5));
        }
        if (statue == 2) {
            list = list(new QueryWrapper<Deliver>().eq("status", 2));
        }
        if (statue == 3) {
            list = list(new QueryWrapper<Deliver>().eq("status", 9));
        }
        return list;
    }

}
